Skip to content

## πŸ“ Author

Birat Aryal β€” birataryal.github.io
Created Date: 2025-06-23
Updated Date: Monday 23rd June 2025 07:56:46
Website - birataryal.com.np
Repository - Birat Aryal
LinkedIn - Birat Aryal
DevSecOps Engineer | System Engineer | Cyber Security Analyst | Network Engineer


🐚 Bash Scripting for Beginners

This guide is a starting point for learning Bash scripting on Linux. It covers basic syntax, variables, conditionals, loops, and useful examples to get you going.

πŸ“ What is Bash?

Bash (Bourne Again SHell) is a Unix shell and command language. Bash scripts allow you to automate tasks, chain commands, and create custom tools.


πŸ“ Getting Started

πŸ”Ή Create a Bash Script

  1. Open a terminal.

  2. Create a file:

Bash
touch hello.sh
  1. Make it executable:
Bash
chmod +x hello.sh
  1. Add a shebang at the top of the file:
Bash
#!/bin/bash

/bin/bash denotes the shell which would be used.


πŸ”€ Basic Script Example

Bash
#!/bin/bash
echo "Hello, World!"

Run it:

Bash
./hello.sh

πŸ”§ Variables

Bash
#!/bin/bash
name="Birat"
echo "Hello, $name!"

πŸ“₯ Read User Input

Bash
#!/bin/bash
echo "What's your name? "
read name
echo "Welcome, $name!"

Tip

You could use on single line to get input from user like this:

Bash
#!/bin/bash
read -p "What's your name? " name
echo "Welcome, $name!"

πŸ”„ Conditionals

Bash
#!/bin/bash
echo "Enter a number:"
read num

if [ "$num" -gt 10 ]; then
    echo "Number is greater than 10"
else
    echo "Number is 10 or less"
fi

Syntax

Bash
if [ condition ]; then
    # if-block
else
    # else-block
fi

πŸ” Loops

For Loop

Bash
#!/bin/bash
for i in 1 2 3 4 5; do
    echo "Number: $i"
done

Syntax

Bash
for variable in list
do
    commands
done

While Loop

Bash
#!/bin/bash
count=1
while [ $count -le 5 ]; do
    echo "Count is $count"
    count=$((count + 1))
done

Syntax

Bash
while [ condition ]
do
    commands
done

πŸ“‚ File Check Example

Bash
#!/bin/bash
file="/etc/passwd"

if [ -f "$file" ]; then
    echo "$file exists."
else
    echo "$file does not exist."
fi

πŸ“‚ Directory Listing with Loop

Bash
#!/bin/bash
for file in *.txt; do
    echo "Found text file: $file"
done

πŸ”§ Functions

Bash
#!/bin/bash
greet() {
    echo "Hello, $1!"
}

greet "Birat"

πŸ“œ Command Line Arguments

Bash
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"

Run with:

Bash
./script.sh one two

🚦 Exit Status

Bash
#!/bin/bash
ls /etc/passwd
echo "Exit status: $?"

πŸ” Sample Real Script: Backup Files

Bash
#!/bin/bash
src_dir="/home/user/docs"
backup_dir="/home/user/backup"

mkdir -p "$backup_dir"
cp -r "$src_dir"/* "$backup_dir"

echo "Backup complete!"

Tips

  • Use chmod +x script.sh to make your script executable.

  • Use set -x to debug your script.

  • Always double quotes variables: "$var"

  • Single quotes does not return the value defined for variables: '$var'

  • Test with bash -n script.sh for syntax checking.


πŸ§ͺ Practice Challenge

Try writing a script that:

  1. Asks for a username.

  2. Checks if the user exists on the system.

  3. If yes, prints their home directory.

  4. Else, prints a message.

πŸ§ͺBasic BashScripting FlowChart

Tip

Based on the diagram below everything could be configured using the bash scripting.

BashScriptingFlowChart